﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using QuantitativeInvestment.Bean;
namespace QuantitativeInvestment.StockPool
{

    class IndexStockPool:StockPool
    {
         public SqlConnection windConnection; // 数据库连接dd
         public SqlCommand windCommand; //数据库连接命令
        public IndexStockPool()
        {
            this.windConnection = new SqlConnection("Server=localhost; User=sa; PWD=111111;database=DFZQ;Asynchronous Processing=true");
            this.windConnection.Open();
            this.windCommand = this.windConnection.CreateCommand();
            this.poolName="指数源";
            Parameter p = new Parameter("指数名", "沪深300");
            p.type = "enum";
            p.enumList.Add("中证100");
            p.enumList.Add("中证200");
            p.enumList.Add("中证500");
            p.enumList.Add("中证700");
            p.enumList.Add("中证800");
            p.enumList.Add("上证综指");
            this.paraList.Add(p.name, p);

        }
        public override List<TradingStock> getStocks(string tradingDate)
        {
            string indexName = this.paraList["指数名"].value.ToString();
            List<TradingStock> stocks = new List<TradingStock>();
            windCommand.CommandText = "SELECT code,name FROM [DFZQ].[dbo].[indexContent],stockList,indexList where indexList.indexCode=indexContent.indexCode and indexContent.stockCode=stockList.code and indexList.indexName like '" + indexName + "' and indexContent.startDate<=" + tradingDate + " and (indexContent.endDate is null or indexContent.endDate>=" + tradingDate + ")";
            SqlDataReader sdr = windCommand.ExecuteReader();
            while (sdr.Read())
            {
                TradingStock stock = new TradingStock(sdr["code"].ToString(), sdr["name"].ToString());
                stocks.Add(stock);
            }
            sdr.Close();
            return stocks;

        }

    }
}
